// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

// 用cc.class 生成一个对象，包含数组皮肤
const cow_skin = cc.Class({
    name:"cow_skin",
    properties:{
        cows:{
            default:[],
            type:[cc.SpriteFrame]
        }
    }
})

cc.Class({
    extends: cc.Component,

    properties: {
        cow_sets: {
            default: [],
            type: [cow_skin]
        },

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.intervalTime = 0;
        this.randomType = Math.floor(Math.random() * 3);


    },

    start () {

    },




    update (dt) {
        // cc.log("update")
        this.intervalTime += dt;
        // 每隔0.2秒更换皮肤
        let index  = Math.floor(this.intervalTime / 0.2);
        // 获取精灵组件
        let sprite = this.node.getComponent(cc.Sprite);
        // let spriteFrame = sprite.spriteFrame;
        // 获取牛的类型
        // let cowSet = this.cow_sets[this.type];
        let cowSet = this.cow_sets[this.randomType];
        // 如果最后一个 重置index
        if (index > cowSet.cows.length -1) {
            index = 0;
            // 重置计时参数
            this.intervalTime = 0;
        }
        // 设置皮肤
        sprite.spriteFrame = cowSet.cows[index]

    },

    runCallback(){
        cc.log("runCallback")
        this.randomType = Math.floor(Math.random() * 3);

    }
});
